﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebSite.Models.UserModel>" %>
<%@ Register src="../Shared/PageTool.ascx" tagname="PageTool" tagprefix="uc_PT" %>
<%@ Import Namespace="WebSite.Models" %>
<%@ Import Namespace="WebSite.App_Code" %>
<%@ Import Namespace="WebSite.App_Data" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Department
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <link href="../../Scripts/jquery-ui/css/redmond/jquery-ui-1.9.2.custom.min.css" rel="stylesheet" />
    <script type="text/javascript" src="../../Scripts/external/jquery.bgiframe-2.1.2.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery-ui-dialog/jquery.ui.core.js"></script>
	<script type="text/javascript" src="../../Scripts/jquery-ui-dialog/jquery.ui.widget.js"></script>
	<script type="text/javascript" src="../../Scripts/jquery-ui-dialog/jquery.ui.position.js"></script>
	<script type="text/javascript" src="../../Scripts/jquery-ui-dialog/jquery.ui.dialog.js"></script>



    <script type="text/javascript">

        $(document).ready(function () {

            if ('<%=ViewData.ContainsKey("DB.Operation.Result") %>' == "True")
                alert('<%=ViewData["DB.Operation.Result"] %>');

        });

        function OpenChangePwd(user_id) {
            $(function () {
                $("#dialog-ChangePwd").dialog({
                    height: 220,
                    width: 400,
                    modal: true
                });
            });

            $("input[name=CP_UserID]").val(user_id)
        }

        function EditRoleRelation(sender, user_id) {

            var all = $("#UR_RoleAll > option");
            var old_ele = $("<select></select>")

            old_ele.append($("select", $(sender).parents("tr:eq(1)")).clone());

            $("#UR_RoleNoSelect").empty();
            $("#UR_RoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);

                if (isOld)
                    $("#UR_RoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#UR_RoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }
                        
            $(function () {
                $("#dialog-UpdateRole").dialog({
                    height: 340,
                    width: 400,
                    modal: true
                });
            });

            $("#UR_UpdateUser").val(user_id);

        }

        function AddRole() {
            var all = $("#UR_RoleAll > option");
            var old_ele = $("<select></select>")
            var new_ele = $("<select></select>")

            old_ele.append($("#UR_RoleSelect > option"));
            new_ele.append($("#UR_RoleNoSelect > option:selected"));

            $("#UR_RoleNoSelect").empty();
            $("#UR_RoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);
                var isNew = ($("option[value=" + all.eq(i).val() + "]", new_ele).length > 0);

                if (isOld || isNew)
                    $("#UR_RoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#UR_RoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }

        }

        function RemoveRole() {
            var all = $("#UR_RoleAll > option");
            var old_ele = $("<select></select>")
            var new_ele = $("<select></select>")

            new_ele.append($("#UR_RoleSelect > option:selected"));
            old_ele.append($("#UR_RoleSelect > option"));

            $("#UR_RoleNoSelect").empty();
            $("#UR_RoleSelect").empty();

            for (var i = 0; i < all.length; i++) {

                var isOld = ($("option[value=" + all.eq(i).val() + "]", old_ele).length > 0);

                if (isOld)
                    $("#UR_RoleSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
                else
                    $("#UR_RoleNoSelect").append("<option value=\"" + all.eq(i).val() + "\">" + all.eq(i).text() + "</option>");
            }

        }

        function MD_TAB_OpenEdit(index) {

            $(".masterdatatable tr[RowType=Display]").show();
            $(".masterdatatable tr[RowType=Update]").hide();
            
            $("#R_D_" + index).hide();
            $("#R_U_" + index).show();
        }

        function OnQuery() { Posting('<%=L_PageEvent.MD_User_Query %>', null); }
        function OnUpdate(id) { Posting('<%=L_PageEvent.MD_User_Upadte %>', id); }
        function OnInsert() { Posting('<%=L_PageEvent.MD_User_Insert %>', null); }
        function OnDelete(id) { Posting('<%=L_PageEvent.MD_User_Delete %>', id); return confirm('是否删除该用户'); }
        function OnEnable(id) { Posting('<%=L_PageEvent.MD_User_Enable %>', id); }
        function OnChangePwd() { Posting('<%=L_PageEvent.MD_User_ChangePwd %>', null); }
        function OnUpdateRole() {
            var sel = $("#UR_RoleSelect > option")
            var context = "";

            for (var i = 0; i < sel.length; i++) {
                context = context + sel.eq(i).val() + ",";
            }

            $("#UR_UpdateRole").val(context);

            Posting('<%=L_PageEvent.MD_User_UpdateRole %>', null); 
        }
        function OnResetPwd(id) { Posting('<%=L_PageEvent.MD_User_ResetPwd %>', id); return confirm('是否要重置密码'); }
             
        function Posting(event, id) {
            $("#PostingEvent").val(event);
            $("#PostingID").val(id);
        }

    </script>

    <% Html.BeginForm(); %>

    <%=Html.HiddenFor(o => o.PostingEvent, new { id = "PostingEvent" })%>
    <%=Html.HiddenFor(o => o.PostingID, new { id = "PostingID" })%>

    <% String PostingID = String.Empty; %>

    <%if (Model.U.HasRole(Role.MGR)) {%>
    <table border="0" cellpadding="5" cellspacing="0">
        <tr>
            <td>姓名</td>
            <td><%=Html.TextBoxFor(o=>o.Query_UserName, new { @class = "budget-txt-no-01" })%></td>
            <td>组织</td>
            <td><%=Html.TextBoxFor(o => o.Query_DepartmentName, new { @class = "budget-txt-no-01" })%></td>
            <td>角色</td>
            <td><%=Html.TextBoxFor(o => o.Query_RoleName, new { @class = "budget-txt-no-01" })%></td>
        </tr>
        <tr>
            <td colspan="6">
                <table border="0" cellpadding="0" cellspacing="0">
                    <td>
                        <div class="budget-button-no-01">
                            <input type="submit" value="查询" hidefocus="true" class="budget-submit-no-01" onclick="Posting('<%=L_PageEvent.MD_User_Query %>')"/>
                        </div>
                    </td>
                    <td>
                        &nbsp;&nbsp;&nbsp;显示无效人员&nbsp;<%=Html.CheckBoxFor(o=>o.Query_ShowAll)%>                       
                    </td>
                </table>
            </td>
        </tr>
    </table>
    <% } %>

    <table border="0" cellpadding="0" cellspacing="1" class="masterdatatable">
        <tr class="masterdatatable_header">
            <td>姓名</td>
            <td>显示名</td>
            <td>组织</td>
            <td>权限</td>
            <td ></td>
        </tr>
            
    <% for (int i = 0; i < Model.Table.Count; i++) { %>
        <% if (Model.Table[i].User.Status) { %> 
            <tr class="masterdatatable_enable" RowType="Display" id="R_D_<%=i %>"> 
        <% } else { %> 
            <tr class="masterdatatable_disable" RowType="Display" id="R_D_<%=i %>">  
        <% } %>
            <td>
                <% PostingID = Guid.NewGuid().ToString(); %>
                <input type="hidden" value="<%=Model.Table[i].User.Guid %>" name="UserID_<%=PostingID %>" />
                <%=Model.Table[i].User.UserName %>
            </td>
            <td>
                <%=Model.Table[i].User.DisplayName %>
            </td>
            <td><%=Model.Table[i].Department.Department.DepartmentName %></td>
            <td>
                <% if (Model.Table[i].RoleList.Count == 0) { %>
                    <select><option>N/A</option></select>
                <% } else { %>
                    <%=Html.DropDownList("Role",Model.Table[i].RoleList.AsEnumerable()) %>
                <% } %>
            </td>
            <td style=" white-space:nowrap;">
                <%if (Model.U.HasRole(Role.MGR)) {%>
                    <%if (Model.Table[i].User.Status) { %>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style=" padding:0px 5px 0px 0px">
                                    <div class="budget-button-no-01">
                                        <input type="button" class="budget-submit-no-01" hidefocus="true" value="修改" onclick="MD_TAB_OpenEdit('<%=i %>');" />
                                    </div>
                                </td>
                                <td style=" padding:0px 5px 0px 0px">
                                <div class="budget-button-no-01">
                                    <input type="submit" class="budget-submit-no-01" hidefocus="true" value="删除" onclick="return OnDelete('<%=PostingID %>')" />
                                </div>
                                </td>
                                <td style=" padding:0px 5px 0px 0px">
                                    <div class="budget-button-no-01">
                                        <input type="button" class="budget-submit-no-01" hidefocus="true" value="修改权限" onclick="EditRoleRelation(this, '<%=Model.Table[i].User.Guid %>')" />
                                    </div>
                                </td>
                                <td style=" padding:0px;">
                                    <div class="budget-button-no-01">
                                        <input type="submit" class="budget-submit-no-01" hidefocus="true" value="重置密码" onclick="return OnResetPwd('<%=PostingID %>')" />
                                    </div>                       
                                </td>
                            </tr>							
							<%if (Model.U.User.Guid == Model.Table[i].User.Guid) { %>
							<tr>
								<td style=" padding:5px 5px 0px 0px" colspan="2">
									<div class="budget-button-no-01">
									<input type="button" class="budget-submit-no-01" hidefocus="true" value="修改密码" onclick="OpenChangePwd('<%=Model.Table[i].User.Guid %>')" />				</div> 
								</td>
							</tr>
							<% } %>
                        </table>                       
                    <% } %>
                    <% else {%>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style=" padding:0px">
                                    <div class="budget-button-no-01" style=" width:50px">
                                        <input type="submit" class="budget-submit-no-01" hidefocus="true" value="启用" onclick="return OnEnable('<%=PostingID %>')" />
                                    </div>
                                </td>
                            </tr>
                        </table>
                    <% } %>
                <% } %>
                <% else { %>
                    <div class="budget-button-no-01">
                        <input type="button" class="budget-submit-no-01" hidefocus="true" value="修改密码" onclick="OpenChangePwd('<%=Model.Table[i].User.Guid %>')" />
                    </div>
                 <% } %>
            </td>
        </tr>
        <% if (Model.Table[i].User.Status) { %> 
            <tr class="masterdatatable_enable" style="display:none" RowType="Update" id="R_U_<%=i %>"> 
        <% } else { %> 
            <tr class="masterdatatable_disable" style="display:none" RowType="Update" id="R_U_<%=i %>">  
        <% } %>
            <td>
                <%=Html.TextBox("U_Name_" + PostingID, Model.Table[i].User.UserName, new { @class = "budget-txt-no-01" })%>
            </td>
            <td>
                <%=Html.TextBox("U_Display_" + PostingID, Model.Table[i].User.DisplayName, new { @class = "budget-txt-no-01" })%>
            </td>
            <td><%=Html.DropDownList("U_Department_" + PostingID, Model.Table[i].DepartmentSource, new { style = "width:150px" })%></td>
            <td>
                <% if (Model.Table[i].RoleList.Count == 0) { %>
                    <select><option>N/A</option></select>
                <% } else { %>
                    <%=Html.DropDownList("Role",Model.Table[i].RoleList.AsEnumerable()) %>
                <% } %>
            </td>
            <td>
                <div class="budget-button-no-01" style=" width:70px;">
                    <input type="submit" value="确认修改" class="budget-submit-no-01" hidefocus="true" onclick="OnUpdate('<%=PostingID %>')" />
                </div>                
            </td>
        </tr>

    <% } %>
    <%if (Model.U.HasRole(Role.MGR)) { %>
        <tr class="masterdatatable_insert"> 
            <td><%=Html.TextBox("I_Name", Model.Insert.User.UserName, new { @class = "budget-txt-no-01" })%></td>
            <td><%=Html.TextBox("I_Display", Model.Insert.User.DisplayName, new { @class = "budget-txt-no-01" })%></td>
            <td><%=Html.DropDownList("I_Department", Model.Insert.DepartmentSource, new { style = "width:150px" })%></td>
            <td></td>
            <td>
                <div class="budget-button-no-01" style="width:50px; text-align:center">
                    <input type="submit" value="添加" class="budget-submit-no-01" hidefocus="true" onclick="OnInsert()" />
                </div>
            </td>
        </tr>
    <%} %>
    </table>

    <%if (Model.U.HasRole(Role.MGR)) { %>
    <br />
    <uc_PT:PageTool ID="ucPage" runat="server" />    
    <% } %>

    <div id="dialog-ChangePwd" title="修改密码" style=" display:none; padding:20px">
            
        <%=Html.HiddenFor(o => o.PostingEvent)%>


        <input name="CP_UserID" type="hidden" />
        <table border="0" cellpadding="5" cellspacing="0" >
            <tr>
                <td>旧密码：</td>
                <td><input type="password" name="CP_Pwd_Old" class="budget-txt-no-01" /></td>
            </tr>
            <tr>
                <td>新密码：</td>
                <td><input type="password" name="CP_Pwd_New" class="budget-txt-no-01"/></td>
            </tr>
            <tr>
                <td>重复密码：</td>
                <td><input type="password" name="CP_Pwd_Confirm" class="budget-txt-no-01"/></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <div class="budget-button-no-01">
                        <input type="submit" class="budget-submit-no-01" hidefocus="true" value="确认修改" onclick="OnChangePwd()" />
                    </div>
                </td>
            </tr>
        </table>

    </div>
    
    <div id="dialog-UpdateRole" title="修改权限" style=" display:none; padding:20px; overflow:hidden">
        
        <%=Html.DropDownList("UR_RoleAll", Model.RoleSource, new { id = "UR_RoleAll", style = "display:none" })%>
        <%=Html.Hidden("UR_UpdateRole")%>
        <%=Html.Hidden("UR_UpdateUser")%>
        <center>
        <br />
        <table>
            <tr>
                <td rowspan="2"><select name="UR_RoleNoSelect" id="UR_RoleNoSelect" multiple="multiple" style="width:125px; height:200px"></select></td>
                <td style=" padding:40px 20px 0px 20px">
                    <div class="budget-button-no-03">
                        <input value=" ＞ " type="button" onclick="AddRole()" class="budget-submit-no-01" hidefocus="true"/>
                    </div>
                </td>
                <td rowspan="2"><select name="UR_RoleSelect" id="UR_RoleSelect" multiple="multiple" style="width:125px; height:200px"></select></td>
            </tr>
            <tr>
                <td style=" padding:0px 20px 20px 20px ">
                    <div class="budget-button-no-03">
                        <input value=" ＜ " type="button" onclick="RemoveRole()" class="budget-submit-no-01" hidefocus="true"/>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="3" style=" text-align:center">
                    <br />
                    <div class="budget-button-no-01" style="width:50px;">
                        <input type="submit" value="保存" onclick="OnUpdateRole()" class="budget-submit-no-01" hidefocus="true" />
                    </div>
                </td>
            </tr>
        </table>
        </center>
    </div>
    
    <% Html.EndForm(); %>

</asp:Content>
